package moderator;

//~--- non-JDK imports --------------------------------------------------------
import SessionFactory.SessionFactoryUtil;

import dto.*;
import java.util.Calendar;

import org.hibernate.SessionFactory;

//~--- JDK imports ------------------------------------------------------------



import java.util.Date;
import java.util.List;
import java.util.Vector;

public class ModeratorControlPanel {

    TopicsHome topicHome = new TopicsHome();
    PostsHome postsHome = new PostsHome();
    UserWarning userWarn = new UserWarning();
    UserWarningHome userWarnHome = new UserWarningHome();
    JfMembersHome memberHome = new JfMembersHome();
    SessionFactory sessionFactory;

//  Session session;
    public ModeratorControlPanel() {
        sessionFactory = SessionFactoryUtil.getInstance();
    }

    public static void main(String[] args) {
        JfMembers mem = new JfMembers(82);
        ModeratorControlPanel mod = new ModeratorControlPanel();
        System.out.print(mod.setBan(true, mem));
    }

//  dina
    public boolean setClose(Topics topic, boolean closeState) {
        boolean result = false;

        SessionFactoryUtil.startNew();
        topic = topicHome.findById(topic.getTopicid());

        if (closeState) {
            topic.setIsclosed(new Integer(1));
        } else {
            topic.setIsclosed(new Integer(0));
        }

        topicHome.merge(topic);

        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

    // dina /////////////////////////////////////////
    public boolean delete(Topics topic) {
        boolean result = false;

        SessionFactoryUtil.startNew();
        topic = topicHome.findById(topic.getTopicid());
        topicHome.delete(topic);

        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

    // dina ////////////////////////////////////////
    public boolean rename(Topics topic, String Newname) {
        boolean result = false;

        SessionFactoryUtil.startNew();

        topic = topicHome.findById(topic.getTopicid());
        topic.setTopicname(Newname);
        topicHome.merge(topic);

        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

    // dina ////////////////////////////////////////////////
    public boolean preventspam(Vector<Posts> post) {
        boolean result = false;
        SessionFactoryUtil.startNew();
        Topics topic = topicHome.findById(post.elementAt(0).getTopics().getTopicid());
        int newPosts = topic.getNumofposts().intValue() - post.size();
        topic.setNumofposts(new Integer(newPosts));
        for (Posts posts : post) {
            postsHome.delete(posts);
        }

        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();

        return result;
    }

    public boolean preventspam(Posts post) {
        boolean result = false;
        FilteredpostsHome filteredHome = new FilteredpostsHome();
        SessionFactoryUtil.startNew();
        post = postsHome.findById(post.getPostid());
        System.out.print("pppppppppppppppppppppp" + post.getTopics().getTopicid());
        Topics topic = topicHome.findById(post.getTopics().getTopicid());
        int newPosts = topic.getNumofposts() - 1;
        topic.setNumofposts(newPosts);
        Filteredposts filteredPost = new Filteredposts();
        System.out.print(post.getContent());
        filteredPost.setContent(post.getContent());
        filteredPost.setFilterdate(new Date());
        filteredPost.setJfMembers(post.getJfMembers());
        filteredPost.setTopics(topic);
        filteredHome.persist(filteredPost);
        postsHome.delete(post);
        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();

        return result;
    }

    // dina////////////////////////////////////////////////////
    public void setSticking(Vector<Topics> topics, boolean stickystate) {
        for (Topics topics2 : topics) {
            if (topics2.getTopicid().equals(new Integer(0))) {
                continue;
            } else {
                setsticking(topics2, stickystate);
            }
        }
    }

    // ziad////////////////////////////////////////////////////////////////
    public boolean move(Integer topicId, Integer sectionId) {
        boolean result = false;
        SessionFactoryUtil.startNew();
        topicHome.updateSection(topicId, sectionId);
        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

//  ziad
    public boolean merge(Vector<Integer> mergedTopicsIds, String newName) {
        boolean result = false;

        SessionFactoryUtil.startNew();
        Topics newTopic = new Topics();

        newTopic.setTopicname(newName);
        //get info of new Topic
        List<Topics> oldTopics = topicHome.getTopicsList(mergedTopicsIds);
        Date creationDate = oldTopics.get(0).getCreationdate();
        Date modificationDate = oldTopics.get(0).getModifydate();
        Integer numofposts = oldTopics.get(0).getNumofposts();
        Integer numlurkers = oldTopics.get(0).getNumlurkers();
        Integer numofviews = oldTopics.get(0).getNumofviews();
        JfMembers creator = oldTopics.get(0).getJfMembers();
        Sections sectionId = oldTopics.get(0).getSections();
        System.out.println("" + oldTopics.size());
        for (int i = 1; i < oldTopics.size(); i++) {
            numofposts += oldTopics.get(i).getNumofposts();
            numlurkers += oldTopics.get(i).getNumlurkers();
            numofviews += oldTopics.get(i).getNumofviews();

            if (creationDate.after(oldTopics.get(i).getCreationdate())) {
                creationDate = oldTopics.get(i).getCreationdate();
                creator = oldTopics.get(i).getJfMembers();
            }
            if (modificationDate.before(oldTopics.get(i).getModifydate())) {
                modificationDate = oldTopics.get(i).getModifydate();
            }
        }
        System.out.println("" + numofposts);
        newTopic.setIsclosed(new Integer(0));
        newTopic.setIspublic(new Integer(1));
        newTopic.setIssticky(new Integer(0));
        newTopic.setNumlurkers(numlurkers);
        newTopic.setNumofposts(numofposts);
        newTopic.setNumofviews(numofviews);
        newTopic.setJfMembers(creator);
        newTopic.setSections(sectionId);
        /////call method to check popularity
        newTopic.setCreationdate(creationDate);
        newTopic.setModifydate(modificationDate);
        topicHome.persist(newTopic);

        int numPosts = postsHome.movePosts(mergedTopicsIds, newTopic.getTopicid());//Num of posts in the topic
        for (int i = 0; i < oldTopics.size(); i++) {
            topicHome.delete(oldTopics.get(i));
        }
        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

//  ziad
    public boolean split(Topics topic, Date splitDate) {

        boolean result = false;
        SessionFactoryUtil.startNew();
        postsHome.deletePostsBeforeDate(topic.getTopicid(), splitDate);
        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

//  dina
    public void setClose(Vector<Topics> topics, boolean closeState) {
        for (Topics topic : topics) {
            if (topic.getTopicid().equals(new Integer(0))) {
                continue;
            } else {
                setClose(topic, closeState);
            }
        }
    }

    // dina ////////////////////////////////////////////////////////////////////
    public boolean setsticking(Topics topic, boolean stickyState) {
        boolean result = false;

        SessionFactoryUtil.startNew();
        topic = topicHome.findById(topic.getTopicid());

        if (stickyState) {
            topic.setIssticky(new Integer(1));
        } else {
            topic.setIssticky(new Integer(0));
        }

        topicHome.attachDirty(topic);

        if (SessionFactoryUtil.commitTrans()) {
            result = true;
        }
        SessionFactoryUtil.finish();
        return result;
    }

    // dina/////////////////////////////////////////////////////////////////////
    public boolean delete(Vector<Topics> topics) {
        boolean result = false;

        for (Topics topics2 : topics) {
            if (topics2.getTopicid().equals(new Integer(0))) {
                continue;
            } else {
                result = delete(topics2);
            }
        }

        return result;
    }

    // yasmeen//////////////////////////////////////////////////////////////////////////
    public boolean setWarn(JfMembers user, JfMembers mod, String msg) {
        SessionFactoryUtil.startNew();
        mod = memberHome.findById(mod.getMemberId());
        user = memberHome.findById(user.getMemberId());
        userWarn.setJfMembersByModeratorId(mod);
        userWarn.setMessage(msg);
        userWarn.setJfMembersByMemberId(user);
        userWarn.setWarningdate(new Date());
        userWarnHome.persist(userWarn);
        boolean b = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        return b;
    }

//  yasmeen
    public boolean setSuspend(int durationDays, JfMembers user, boolean suspenedState) {

        Calendar cal = Calendar.getInstance();
        Date d = cal.getTime();
        System.out.println("I am in susbend");
        boolean res = false;
        SessionFactoryUtil.startNew();
        user = memberHome.findById(user.getMemberId());
        if (!suspenedState) {
            user.setUnsuspenddate(null);
            memberHome.merge(user);
            System.out.print("insertion done");
        } else if (suspenedState) {
            d.setDate(cal.getTime().getDate() + durationDays);
            user.setUnsuspenddate(d);
            memberHome.merge(user);
            System.out.print("insertion done");
        }
        res = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        return res;
    }
////////////////////////yasmine////////////////////////

    public boolean setSuspend(Vector<JfMembers> members, boolean suspenedState, int durationDays) {
        boolean sus = false;
        for (JfMembers member : members) {
            if (member.getUnsuspenddate().equals(null)) {
                memberHome.merge(member);
                sus = true;
                continue;
            } else {
                member.setUnsuspenddate(new Date(durationDays));
                memberHome.merge(member);
                sus = false;
            }
        }
        return sus;
    }
    // yasmine//////////////////////////////////////////////////////////////

    public boolean setBan(boolean ban, JfMembers user) {
        System.out.println("i am in setBan");
        boolean b = false;
        SessionFactoryUtil.startNew();
        System.out.print(user.getMemberId() + "$$$$h$$$$$$$$$");
        user = memberHome.findById(user.getMemberId());
        System.out.print("*****************************" + user.getEMail());
        if (!ban) {

            user.setBanned(0);
            memberHome.merge(user);
            System.out.println("ban done");
        } else if (ban) {
            user.setBanned(1);
            memberHome.merge(user);
            System.out.println("ban done ");
        }
        b = SessionFactoryUtil.commitTrans();
        SessionFactoryUtil.finish();
        return b;
    }
    /////////////////yasmine////////////////////////////////////

    public void setBan(Vector<JfMembers> members, boolean ban) {
        boolean bann = false;
        for (JfMembers member : members) {
            if (member.getBanned().equals(0)) {
                memberHome.merge(member);
                bann = true;
                continue;
            } else {
                member.setBanned(1);
                memberHome.merge(member);
                bann = false;
            }
        }
    }

    // dina ///////////////////////////////////////////////////////////////
    public boolean checkprevelage(Integer sectionId, JfMembers member) {
        boolean result = false;

        SessionFactoryUtil.startNew();
        result = topicHome.checkprevelage(sectionId, member);
        System.out.print(result);
        SessionFactoryUtil.finish();

        return result;
    }

    public Vector<JfMembers> getAllUsers() {
        SessionFactoryUtil.startNew();
        System.out.print("inside getallusers in modcontrolpanel");
        Vector<JfMembers> users = new Vector<JfMembers>();
//        memberHome.startNew();
        users = memberHome.getAllUsers();
//        memberHome.finish();
        SessionFactoryUtil.finish();
        return users;
    }
}
//~ Formatted by Jindent --- http://www.jindent.com

